const { expect } = require('chai');
const BugReporter = require('../src/BugReporter.js').default;

describe('BugReporter', () => {
    let bugReporter;
    const testIssueURL = 'https://gitcode.com/test/repo/issues/new';

    beforeEach(() => {
        bugReporter = new BugReporter(testIssueURL);
    });

    describe('constructor', () => {
        it('should initialize with correct default values', () => {
            expect(bugReporter.issueURL).to.equal(testIssueURL);
            expect(bugReporter.title).to.equal('');
            expect(bugReporter.body).to.equal('');
            expect(bugReporter.labels).to.equal('BugReport');
        });
    });

    describe('setTitle', () => {
        it('should set the title correctly', () => {
            const testTitle = 'Test Issue Title';
            bugReporter.setTitle(testTitle);
            expect(bugReporter.title).to.equal(testTitle);
        });

        it('should handle empty title', () => {
            bugReporter.setTitle('');
            expect(bugReporter.title).to.equal('');
        });

        it('should handle null/undefined title', () => {
            bugReporter.setTitle(null);
            expect(bugReporter.title).to.equal('');
            
            bugReporter.setTitle(undefined);
            expect(bugReporter.title).to.equal('');
        });
    });

    describe('setBody', () => {
        it('should set the body correctly', () => {
            const testBody = 'Test issue body content';
            bugReporter.setBody(testBody);
            expect(bugReporter.body).to.equal(testBody);
        });

        it('should handle empty body', () => {
            bugReporter.setBody('');
            expect(bugReporter.body).to.equal('');
        });

        it('should handle null/undefined body', () => {
            bugReporter.setBody(null);
            expect(bugReporter.body).to.equal('');
            
            bugReporter.setBody(undefined);
            expect(bugReporter.body).to.equal('');
        });
    });

    describe('getSelectedText', () => {
        it('should return empty string when no selection', () => {
            // Mock window.getSelection to return empty selection
            const mockSelection = {
                toString: () => ''
            };
            global.window = {
                getSelection: () => mockSelection
            };
            
            const result = bugReporter.getSelectedText();
            expect(result).to.equal('');
        });

        it('should return selected text when selection exists', () => {
            const selectedText = 'Selected text content';
            const mockSelection = {
                toString: () => selectedText
            };
            global.window = {
                getSelection: () => mockSelection
            };
            
            const result = bugReporter.getSelectedText();
            expect(result).to.equal(selectedText);
        });

        it('should handle document.selection for older browsers', () => {
            const selectedText = 'Selected text from document.selection';
            const originalWindow = global.window;
            const originalDocument = global.document;
            
            global.window = {
                getSelection: () => null
            };
            global.document = {
                selection: {
                    type: 'Text',
                    createRange: () => ({
                        text: selectedText
                    })
                }
            };
            
            const result = bugReporter.getSelectedText();
            expect(result).to.equal(selectedText);
            
            // Restore original values
            global.window = originalWindow;
            global.document = originalDocument;
        });
    });

    describe('report', () => {
        beforeEach(() => {
            // Mock window.open
            global.window = {
                ...global.window,
                open: () => {}
            };
        });

        it('should generate correct URL with all parameters', () => {
            const testTitle = 'Test Issue';
            const testBody = 'Test body content';
            
            bugReporter.setTitle(testTitle);
            bugReporter.setBody(testBody);
            
            let openedURL = '';
            global.window.open = (url) => {
                openedURL = url;
            };
            
            bugReporter.report();
            
            expect(openedURL).to.include(testIssueURL);
            expect(openedURL).to.include('title=');
            expect(openedURL).to.include('body=');
            expect(openedURL).to.include('labels=BugReport');
        });

        it('should handle empty title and body', () => {
            let openedURL = '';
            global.window.open = (url) => {
                openedURL = url;
            };
            
            bugReporter.report();
            
            expect(openedURL).to.include(testIssueURL);
            expect(openedURL).to.include('title=');
            expect(openedURL).to.include('body=');
            expect(openedURL).to.include('labels=BugReport');
        });
    });
});